package andy.demo.net.networkinfo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class NetworkReceiver extends BroadcastReceiver {

	private Context mContext;

	@Override
	public void onReceive(Context context, Intent intent) {

		mContext = context;

		// Type 1
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		NetworkInfo mobileNetInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		showToast("Active Network Type", activeNetInfo);
		showToast("Mobile Network Type", mobileNetInfo);

		// Type 2

		// 第二种
		NetworkInfo networkInfo = intent
				.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
		String extraInfo = intent
				.getStringExtra(ConnectivityManager.EXTRA_EXTRA_INFO);
		boolean isFailOver = intent.getBooleanExtra(
				ConnectivityManager.EXTRA_IS_FAILOVER, false);
		boolean noConnectivity = intent.getBooleanExtra(
				ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
		NetworkInfo otherNetworkInfo = intent
				.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
		String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
		//
		showToast("networkInfo", networkInfo);
		showToast("extraInfo", extraInfo);
		showToast("isFailOver", isFailOver);
		showToast("noConnectivity", noConnectivity);
		showToast("otherNetworkInfo", otherNetworkInfo);
		showToast("reason", reason);
	}

	public void showToast(String info, Object object) {
		if (object == null) {
			return;
		}
//		Toast.makeText(mContext, info + " : " + object.toString(), 0).show();
		System.out.println(info + ":" + object.toString() );
	}

}
